package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import java.text.DecimalFormat;

import android.content.Context;
import android.os.StatFs;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

public class InfoSGView {
    private static final String SG_KEYS[] = {
            DeviceInfoKey.K.SG_INT_SIZE,
            DeviceInfoKey.K.SG_INT_PATH,
            DeviceInfoKey.K.SG_EXT_NUM,
            DeviceInfoKey.K.SG_1ST_EXT_SIZE,
            DeviceInfoKey.K.SG_1ST_EXT_PATH,
            DeviceInfoKey.K.SG_1ST_EXT_REMOVABLE,
            DeviceInfoKey.K.SG_1ST_EXT_EMULATED,
            DeviceInfoKey.K.SG_2ND_EXT_SIZE,
            DeviceInfoKey.K.SG_2ND_EXT_PATH
    };
    
    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoSGView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
    public View
    getView(boolean for_preview)
    {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);
        
        String total_src = _cxt.getResources().getString(R.string.di_sg_total) + " ";
        String avail_src = _cxt.getResources().getString(R.string.di_sg_avail) + " ";
        for (int i=0; i<SG_KEYS.length; i++) {
            String info = _di.GetString(SG_KEYS[i]);
            if (!TextUtils.isEmpty(info) && !info.equals(CONSTS.UNAVAILABLE)) {
                View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item,null);
                InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
                row.setTag(wrapper);
                wrapper.setItemName(DeviceInfoKey.GetName(SG_KEYS[i]));
        		if (TextUtils.equals(SG_KEYS[i], DeviceInfoKey.K.SG_INT_SIZE)) {
        			info = total_src +
        					getSizeString(Long.parseLong(_di.GetString(SG_KEYS[i])));
        			if (for_preview == false) {
        				info += "\n" +
    					avail_src +
    					getAvailableSize(_di.GetString(DeviceInfoKey.K.SG_INT_PATH));
        			}
        		}
        		else if (TextUtils.equals(SG_KEYS[i], DeviceInfoKey.K.SG_1ST_EXT_SIZE)) {
        			info = total_src + getSizeString(Long.parseLong(_di.GetString(SG_KEYS[i])));
        			if (for_preview == false) {
        				info += "\n" +
						avail_src +
						getAvailableSize(_di.GetString(DeviceInfoKey.K.SG_1ST_EXT_PATH));
        			}
        		} else if (TextUtils.equals(SG_KEYS[i], DeviceInfoKey.K.SG_2ND_EXT_SIZE)) {
        			info = total_src +
        					getSizeString(Long.parseLong(_di.GetString(SG_KEYS[i])));
        			if (for_preview == false) {
        				info += "\n" +
						avail_src +
						getAvailableSize(_di.GetString(DeviceInfoKey.K.SG_2ND_EXT_PATH));
        			}
        		}
                wrapper.setItemDetail(info);
                sv.addView(row);
            }               
        }
        return sv;
    }
    
    private String
    getSizeString(long size)
    {
    	String info;
		long gb_v = size / (InfoViewCONSTS.GB_LV); 
		if (gb_v > 0) {
			double mi_v = ((double)size)/((double)InfoViewCONSTS.GB_LV);
			info = new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_gb);
		}
		else {
			double mi_v = ((double)size)/((double)InfoViewCONSTS.MB_LV);
			info = new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_mb);
		}	        			
    	return info;
    }
    
    private String
    getAvailableSize(String str_path)
    {
        long lSize = 0;
        try {
        	StatFs stat = new StatFs(str_path);
        	int blockSize = stat.getBlockSize();
        	int totalFreeBlocks = stat.getAvailableBlocks();
        	lSize = ((long)blockSize) * totalFreeBlocks;
        } catch (Exception e) {
            /*
            Caused by: java.lang.IllegalArgumentException
            at android.os.StatFs.native_setup(Native Method)
            at android.os.StatFs.<init>(StatFs.java:32)
            */
        }
        return getSizeString(lSize);
    }
}
